package com.sdgakj.service.commonReport;

import com.google.common.collect.Maps;
import com.sdgakj.dto.CommentDetailDTO;
import com.sdgakj.entity.CommonReview;
import com.sdgakj.enums.EnumReviewType;
import com.sdgakj.repository.CommonReviewDao;
import com.sdgakj.utils.SchoolSearchFilter;
import com.sdgakj.utils.SchoolSpecifications;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Component
@Transactional
public class CommonReviewService {
	
	
	private static Logger logger = LoggerFactory.getLogger(CommonReviewService.class);

	@Autowired
	private CommonReviewDao reviewDao;
	

	
	/**
	 * 查询列表
	 * @param userType
	 * @param searchParams
	 * @param pageNumber
	 * @param pageSize
	 * @param sortType
	 * @return
	 */
	public Page<CommonReview> getReview(Map<String,Object> searchParams, int pageNumber, int pageSize,
										String sortType){
		PageRequest pageRequest =buildPageRequest(pageNumber,pageSize,sortType);
		Specification<CommonReview> spec =buildSpecification(searchParams);
		
		return reviewDao.findAll(spec,pageRequest);
	}
	
	/**
	 * 保存信息
	 * @param entity
	 */
	public void saveReview(CommonReview entity) {
		reviewDao.save(entity);
	}
	
	/**
	 * 更新举报信息
	 * @param entity
	 */
	public void updateReview(CommonReview entity) {
		reviewDao.save(entity);
	}
	
	/**
	 * 删除
	 * @param id
	 */
	public void deleteReview(Long id) {
		CommonReview entity = this.getReviewById(id);
		reviewDao.delete(entity);
	}
	
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	public CommonReview getReviewById(Long id) {
		return  reviewDao.findOne(id);
	}
	
	/**
	 * 分页查询列表
	 * @param searchParams
	 * @param pageNumber
	 * @param pageSize
	 * @param sortType
	 * @return
	 */
	public Page<CommonReview> getPageReview(Map<String,Object> searchParams, int pageNumber, int pageSize, String sortType){
		PageRequest pageRequest =buildPageRequest(pageNumber,pageSize,sortType);
		Specification<CommonReview> spec =buildSpecification(searchParams);
		
		return reviewDao.findAll(spec,pageRequest);
	}
	
	
	/**
	 * 分页查询列表
	 * @param searchParams
	 * @param pageNumber
	 * @param pageSize
	 * @param sortType
	 * @return
	 */
	public List<CommonReview> getReviewList(String deltag, String sortName, String sortType){
		Map<String, SearchFilter> filters = Maps.newHashMap();
		if (StringUtils.isNotBlank(deltag)) {
			filters.put("deltag", new SearchFilter("deltag", Operator.EQ, deltag));
		}
		Specification<CommonReview> spec = DynamicSpecifications.bySearchFilter(filters.values(), CommonReview.class);
		Sort sort = buildSortRequest(sortName,sortType);
		return reviewDao.findAll(spec,sort);
	}
	
	
	/**
	 * 创建排序.
	 */
	private Sort buildSortRequest(String sortName,String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		}else if("desc".equals(sortType)){
			sort = new Sort(Direction.DESC, sortName);
		}else{
			sort = new Sort(Direction.ASC, sortName);
		}
		return sort;
	}
	
	
	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		}else{
			sort=new Sort(Direction.ASC,"orderno");
		}

		return new PageRequest(pageNumber - 1, pagzSize, sort);
	}
	
	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<CommonReview> buildSpecification(Map<String, Object> searchParams) {
		Map<String, SchoolSearchFilter> filters = SchoolSearchFilter.parse(searchParams);
		Specification<CommonReview> spec = SchoolSpecifications.bySearchFilter(filters.values(),CommonReview.class);
		return spec;
	}
	
	public List<CommonReview> getReviewFeed(Long targetId) {
		int type = EnumReviewType.FEED.getCode();//获取审核的帖子信息
		return reviewDao.findByTargetIdAndType(targetId, type);
	}
	
	/**
	 * 查询贴子评论的详细信息
	 * @return
	 */
	public List<CommentDetailDTO> findReviewByFeedId(Long feedId){
		int type= EnumReviewType.FEEDCOMMENT.getCode();
		List<Object[]> result=reviewDao.findReviewByFeedId(type, feedId);
		List<CommentDetailDTO> list=new ArrayList<CommentDetailDTO>();
		if(CollectionUtils.isNotEmpty(result)){
			for(int i =0;i<result.size();i++){
				CommentDetailDTO detailDTO=new CommentDetailDTO();
				detailDTO.setNote(String.valueOf(result.get(i)[0]));
				detailDTO.setCreateDate(String.valueOf(result.get(i)[1]));
				detailDTO.setOperator(String.valueOf(result.get(i)[2]));
				detailDTO.setMcontent(String.valueOf(result.get(i)[3]));
				detailDTO.setUserId(Long.valueOf(result.get(i)[4].toString())); //发表评论的用户id
				detailDTO.setMlogin(String.valueOf(result.get(i)[5]));
				list.add(detailDTO);
			}
		}
		return list;
	}
}
